<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>方案1：互动后播放</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <div class="container">
    <div class="video-wrapper" style="position:relative;display:inline-block;">
      <video id="video" width="100%" src="./videoDemo.mp4" controls></video>
      <div id="modal" class="mask" style="display:none;">
        <button id="playBtn" class="play-btn">点击播放</button>
      </div>
    </div>
  </div>
</body>
<script>
  const video = document.getElementById('video');
  const playBtn = document.getElementById('playBtn');
  const modal = document.getElementById('modal');

  // 页面加载后尝试自动播放
  play();

  async function play() {
    try {// 尝试自动播放视频
      await video.play();// 尝试播放视频
      modal.style.display = 'none';// 隐藏遮罩
      playBtn.removeEventListener('click', play);// 移除按钮的点击事件监听器
    } catch (error) {// 捕获自动播放失败的错误
      // 自动播放失败，显示遮罩和按钮
      modal.style.display = 'flex';
      playBtn.addEventListener('click', play);
    }
  }
</script>
<style>
  .container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .video-wrapper {
    width: 60vw;
    /* max-width: 600px;
    min-width: 300px; */
    position: relative;
  }
  video {
    width: 100%;
    height: auto;
    display: block;
  }
  .mask {
    position: absolute;
    left: 0; top: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2;
    flex-direction: column;
  }
  .play-btn {
    padding: 14px 32px;
    font-size: 18px;
    color: #fff;
    background: #ff6c0e;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  }
</style>
</html>